package com.task;

import com.entity.SysJob;
import com.utils.StringUtil;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class JavaClassTaskInvok implements ITaskInvok {


    /***
     * 得到class执行的定时任务
     * @param job
     */
    @Override
    public void invokMethod(SysJob job) {

        Object obj;
        Class clazz;
        Method method;
        Object returnValue;
        try {
            if (StringUtil.isNotEmpty(job.getMethodParamsValue()) ){
                clazz = Class.forName(job.getClassName());
                Class<?>[] clazzInterfaces = clazz.getInterfaces();

                //通过反射实例化执行的类
                obj = clazz.newInstance();


                method = clazz.getDeclaredMethod(job.getMethodName(), String.class);
                returnValue = method.invoke(obj, job.getMethodParamsValue());
            } else  {
                clazz = Class.forName(job.getClassName());
                Class<?>[] clazzInterfaces = clazz.getInterfaces();

                //通过反射实例化执行的类
                obj = clazz.newInstance();


                method = clazz.getDeclaredMethod(job.getMethodName(), String.class);
                returnValue = method.invoke(obj);
            }


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}
